; drawfun.asm
; Window drawing functions
;

	.include "common.inc"
	.include "global.inc"
	.include "window.inc"
	.include "graphic.inc"
	.include "text.inc"
	.include "cursor.inc"
	
	.export drw_window
	.export drw_button
	.export drw_edit
	.export upd_btnst
	.export drw_wintxt
	.export edrawbeam
	.export drwwintxt

	.export style
	.export top
	.export left
	.export tdx
	.export tdy
	.export text
	
	.segment "DATA"
	
style:		.byte $00
top:		.byte $00
left:		.byte $00
sx:			.byte $00
sy:			.byte $00
tdx:		.byte 0
tdy:		.byte 0
text:		.word $0000
	
	.segment "CODE"
	
drw_window:	jsr win_begindraw
			jsr win_getstyle
			sta style
			; get top/left
			jsr win_getpos
			stx left
			sty top
@rtdone:	lda #WSTYLE_BCKGND
			bit style
			beq @bkend
			; draw background
			lda #GM_SET
			jsr g_mode
			jsr topleft
			jsr win_getdim
			jsr g_block
@bkend:		lda #GM_CLEAR
			jsr g_mode
			lda #WSTYLE_BORDER
			bit style
			beq @skpborder
			; draw border
			jsr topleft
			jsr win_getdim
			jsr g_rect
@skpborder:	lda #WSTYLE_HASTITLE
			bit style
			beq @skptitle
			; draw title bar
			jsr topleft
			jsr win_getdim
			ldy #WIN_TITLE_HEIGHT
			jsr g_block
			inc top
			lda #WSTYLE_TXTOP
			ora style
			sta style
			lda #GM_SET
			jsr g_mode
@skptitle:	jsr win_gettext
			sta text
			stx text+1
			ora text+1
			beq @notext
			; draw text
			jsr win_getfont
			jsr t_font
			jsr getwdelta
			jsr drwwintxt
@notext:	rts

prepwintxt:	jsr win_getstyle
			sta style
			jsr win_gettext
prptxcoord: sta text
			stx text+1
			jsr win_getpos
			stx left
			sty top
			jsr getwdelta
			rts

drw_wintxt: jsr prptxcoord
			jsr win_getstyle
			sta style
			jmp drwwintxt

topleft:	ldx left
			ldy top
			jmp g_moveto

;
;	Puts window dimensions in dx/dy
;

getwdelta:	jsr win_getdim
			stx tdx
			sty tdy
			rts

;
;	Calculates text position
;	INPUT:
;		style ... Text position
;		text .... Text ptr
;		left .... Left coord of text output area
;		right ... Top coord of test output area
;		dx ...... Width of text output area
;		dy ...... Height of text output area
;	OUTPUT:
;		sx/sy ... Rel. top left corner of text rectangle
;

clctxtpos:	lda text
			ldx text+1
			jsr t_getextent
			stx sx
			sty sy
			ldx tdx
			ldy tdy
			; calc hor. pos. (sx)
			lda #WSTYLE_TXLEFT	; align left?
			bit style
			beq @noleft
			; left
			lda #0
			sta sx
			jmp @horend
@noleft:	txa
			sec
			sbc sx
			sta sx
			lda #WSTYLE_TXRIGHT
			bit style
			bne @horend
			lsr sx
@horend:	; calc vert. pos. (sy)
			lda #WSTYLE_TXTOP
			bit style
			beq @notop
			lda #0
			sta sy
			jmp @verend
@notop:		tya
			sec
			sbc sy
			sta sy
			lda #WSTYLE_TXBOTTOM
			bit style
			bne @verend
			lsr sy
@verend:	rts

;
;	Draws text
;	INPUT:
;		style ... Text position
;		text .... Text ptr
;		left .... Left coord of text output area
;		right ... Top coord of test output area
;		dx ...... Width of text output area
;		dy ...... Height of text output area

drwwintxt:	jsr clctxtpos	; get text size
			clc
			lda sx
			adc left
			tax
			lda sy
			adc top
			tay	
			jsr g_moveto
@drawtext:	lda text
			ldx text+1
			jsr t_drawtext
			rts

drw_button: jsr drw_window
			jsr win_getstate
			beq btnend
tgl_btnst:	jsr win_getpos
			inx
			iny
			jsr g_moveto
			lda #GM_XOR
			jsr g_mode
			jsr win_getdim
			dex
			dex
			dey
			dey
			jsr g_block
btnend:		rts

upd_btnst:	jsr win_setstate
			jsr cur_hide
			jsr win_begindraw
			jsr tgl_btnst
			jmp cur_show

	.segment "DATA"

editbuf:	.res 80
	
	.segment "CODE"

drw_edit:	jsr drw_window
			jsr win_getstate
			tax
			and #EDIT_HAS_FOCUS
			bne @drw_beam
			rts
@drw_beam:	txa
edrwbeami:	and #EDIT_POS_MASK
			ldx text
			stx reg2
			ldx text+1
			stx reg2+1
			ldy #0
			cmp (reg2), y
			bcc @inrange
			rts
@inrange:	ldx #<editbuf
			stx reg1
			stx text
			ldx #>editbuf+1
			stx reg1+1
			stx text+1
			sta (reg1), y
			tay
			iny
@cplp:		lda (reg2), y
			sta (reg1), y
			dey
			bne @cplp
			lda #GM_XOR
			jsr g_mode
			lda text
			ldx text+1
			jsr t_getextent
			tya
			pha
			txa
			clc
			adc sx
			adc left
			tax
			lda sy
			adc top
			tay
			dey
			jsr g_moveto
			pla
			tay
			iny
			iny
			jsr g_vline
			rts

edrawbeam:	pha
			jsr win_begindraw
			jsr win_getfont
			jsr t_font
			jsr prepwintxt
			jsr clctxtpos
			jsr cur_hide
			pla
			jsr edrwbeami
			jmp cur_show

